package cn.gok.mybatisplus.controller;

import cn.gok.mybatisplus.dao.StudentDao;
import cn.gok.mybatisplus.entity.Student;
import cn.gok.mybatisplus.entity.User;
import cn.gok.mybatisplus.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName StudentController
 * @Description TODO
 * @Author HP
 * @Date 14:30 2022/7/22
 * @Version 1.0
 **/
@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    private ModelAndView modelAndView = new ModelAndView();

    //查询所有班级信息
    @RequestMapping("/selectAll")
    public ModelAndView selectAll() {
        List<Student> list = studentService.list();
        modelAndView.addObject("studentList", list);
        modelAndView.addObject("message", "找到" + list.size() + "条记录");
        modelAndView.setViewName("studentList");
        return modelAndView;
    }

    //根据id查询学生信息
    @RequestMapping("/selById")
    public ModelAndView selById(@RequestParam Long sid) {
        if (sid == null) {
            modelAndView.setViewName("redirect:/student/selectAll");
        } else {
            Student student = studentService.selectMyOne(1L);
            modelAndView.addObject("studentList", student);
            if (student == null) {
                modelAndView.addObject("message", "没有找到记录");
            } else {
                modelAndView.addObject("message", "找到1条记录");
            }
            modelAndView.setViewName("studentList");
        }
        return modelAndView;
    }

    //添加或更新学生信息
    @RequestMapping(value = "addOrUpdate", method = RequestMethod.POST)
    public ModelAndView add(Student student) {
        try {
            modelAndView.addObject("status_message", studentService.saveOrUpdate(student) ? "新增或修改：" + student.getSname() : "操作失败");
        } catch (Exception e) {
            SQLException se = (SQLException) e.getCause();
            se.getErrorCode();
            modelAndView.addObject("status_message", "发生错误！");
        } finally {
            modelAndView.setViewName("redirect:/student/selectAll");
            return modelAndView;
        }
    }

    //根据id删除学生
    @RequestMapping("del")
    public ModelAndView del(Long sid) {
        studentService.removeById(sid);
        modelAndView.addObject("status_message", "删除：" + sid);
        modelAndView.setViewName("redirect:/student/selectAll");
        return modelAndView;
    }

    //模糊查询
    @RequestMapping("selAllLike")
    public ModelAndView selectAllClazzAndStudent(Student s) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("sname", s.getSname());
        List<Student> students = studentService.listMaps(queryWrapper);
        System.out.println(students);
        modelAndView.addObject("studentList",students);
        modelAndView.addObject("message", "找到"+students.size()+"条记录");
        modelAndView.setViewName("studentList");
        return modelAndView;
    }
    //分页查询
    @RequestMapping("selPage")
    public ModelAndView selPage(Integer pr1,Integer pr2) {
        //limit x,y 从x行开始，要y个
        //参数1 当前页，参数2 当前页面数据的行数
        if(pr1==null || pr2==null){
            modelAndView.addObject("status_message", "请检查输入内容");
            modelAndView.setViewName("redirect:/student/selectAll");
        }else {
            Page<Student> page=new Page<>(pr1,pr2);
            IPage<Student> iPage=studentService.page(page,null);
            modelAndView.addObject("studentList",iPage.getRecords() );
            modelAndView.addObject("status_message", "分页："+pr1+"-"+pr2);
            modelAndView.addObject("message", "共" + page.getTotal() + "条记录");
            modelAndView.setViewName("studentList");
        }
        return modelAndView;
    }
    //批量删除班级(手动测试)
    @RequestMapping("delStudents")
    @ResponseBody
    public String delStudents(@RequestBody List<Long> ids) {
        return studentService.removeByIds(ids) ? "删除成功" : "删除失败";
    }
}
